<?php


namespace app\admin\controller;


use think\Db;

class UserRequest
{
    public function add_userRequest(){

        $fromid=$_POST['fromid'];
        $fromName=$_POST['fromName'];
        $toid=$_POST['toid'];
        $appendMessage=$_POST['appendMessage'];
        $isAgree=$_POST['isAgree'];
        $isRead=$_POST['isRead'];
        $isRefuse=$_POST['isRefuse'];

        //如果这个人已经在申请表里面则 不添加数据
        $result=Db::table("userRequest")->where("fromid",$fromid)->where("toid",$toid)->find();
        if($result != null){
            $fail=['state'=>200,'type'=>"areadyhave"];
            echo json_encode($fail,JSON_UNESCAPED_UNICODE);
            return ;
        }


        //添加数据申请表
        $arr=["fromid"=>$fromid,"fromName"=>$fromName,"toid"=>$toid,"appendMessage"=>$appendMessage,"isAgree"=>$isAgree,"isRead"=>$isRead,"isRefuse"=>$isRefuse];
        $effact_row=Db::table("userRequest")->insert($arr);
        if($effact_row){
            $success=['state'=>200,'type'=>"success"];
            echo json_encode($success,JSON_UNESCAPED_UNICODE);
        }else{
            $fail=['state'=>400,'type'=>"fail"];
            echo json_encode($fail,JSON_UNESCAPED_UNICODE);
        }
    }

    //查询有谁想添加我
    public function select_userRequest(){
        $fromid=$_POST['fromid'];
        $res_arr=Db::table("userRequest")->where("toid",$fromid)->where("isAgree","false")->where("isRefuse","false")->order('id','desc')->select();
        echo json_encode($res_arr,JSON_UNESCAPED_UNICODE);
    }

    public function delete_item_userRequest(){
            $fromid=$_POST['fromid'];
            $fromName=$_POST['fromName'];
            $effect_row=Db::table("userRequest")->where("fromid",$fromid)->where("fromName",$fromName)->delete();
            if($effect_row){
                $res=array("state"=>200,"type"=>"success");
                echo json_encode($res,JSON_UNESCAPED_UNICODE);
            }else{
                $res=array("state"=>400,"type"=>"fail");
                echo json_encode($res,JSON_UNESCAPED_UNICODE);
            }
    }
}